<template>
  <div>
    <div class="item">
      <img src="@/assets/list/like.png" @click="like" v-if="!info.is_love" alt="" class="like">
      <img src="@/assets/list/no-like.png" @click="like" v-else alt="" class="like">
      <div class="item__img">
        <img @click.stop="img(info.image_urls)" :src="info.image_urls" alt="">
      </div>
      <div class="item__name">{{ info.product_name }}</div>
      <div class="item__des">
      <span class="item__des-name">{{
          info.product_material
        }}{{ info.product_purity && info.product_purity != "null" ? info.product_purity : "" }}</span>
        <span class="item__des-unit">{{ info.material_weight }}g</span>
      </div>
      <div class="item__bottom">
        <img @click="addBus" src="@/assets/list/addBus.png" alt="" class="bay">
      </div>
    </div>
    <van-popup position="bottom" v-model:show="show">
      <div class="mask">
        <div class="mask__title">订货</div>
        <div class="mask__good">
          <img class="mask__good-img"  @click="img(info.image_urls.split(',')[0])" :src="info.image_urls.split(',')[0]" alt=""/>
          <div class="mask__good-des">
            <span class="des__name">{{ info.product_name }}</span>
            <div class="des_des">
              <span>{{ info.product_material }}{{ info.product_purity }}</span>
              <span>{{ info.material_weight }}g</span>
            </div>
          </div>
        </div>
        <div class="mask__chooseItem" v-if="detial.product_material.length">
          <div class="mask__chooseItem-title">产品材料</div>
          <div class="mask__chooseItem-list">
            <div class="list__item" v-for="item,index in detial.product_material"
                 @click="chooseItem('product_material',index)" :class="item.check?'active':''">
              {{ item.label }}
            </div>
          </div>
        </div>
        <div class="mask__chooseItem" v-if="detial.product_purity.length">
          <div class="mask__chooseItem-title">产品纯度</div>
          <div class="mask__chooseItem-list">
            <div class="list__item" v-for="item,index in detial.product_purity"
                 @click="chooseItem('product_purity',index)" :class="item.check?'active':''">
              {{ item.label }}
            </div>
          </div>
        </div>
        <div class="mask__chooseItem" v-if="detial.product_technology.length">
          <div class="mask__chooseItem-title">产品工艺</div>
          <div class="mask__chooseItem-list">
            <div class="list__item" v-for="item,index in detial.product_technology"
                 @click="chooseItem('product_technology',index)" :class="item.check?'active':''">
              {{ item.label }}
            </div>
          </div>
        </div>
        <div class="mask__chooseItem" v-if="detial.product_skn.length">
          <div class="mask__chooseItem-title">表面处理</div>
          <div class="mask__chooseItem-list">
            <div class="list__item" v-for="item,index in detial.product_skn"
                 @click="chooseItem('product_skn',index)" :class="item.check?'active':''">
              {{ item.label }}
            </div>
          </div>
        </div>
        <div class="mask__chooseItem" v-if="detial.material_weight.length">
          <div class="mask__chooseItem-title">重量</div>
          <div class="mask__chooseItem-list">
            <div class="list__item" v-for="item,index in detial.material_weight"
                 @click="chooseItem('material_weight',index)" :class="item.check?'active':''">
              {{ item.label }}
            </div>
          </div>
        </div>

        <div class="mask__number">
          <span>数量</span>
          <div>
            <van-stepper v-model="value" step="1"/>
          </div>
        </div>
        <div class="mask__footer">
          <div class="mask__footer-left" @click="reset">重置</div>
          <div class="mask__footer-right" @click="confirmAdd">加入购物车</div>

        </div>
      </div>
    </van-popup>

  </div>
</template>

<script setup>
import {cartEdit, love, product} from "../../../../api/index.js";
import {useRouter} from "vue-router";
import {Toast} from "vant";
import { ImagePreview } from 'vant';

import {ref} from "vue";

const props = defineProps(['info', 'init'])
const router = useRouter()
const show = ref(false)
const detial = ref({
  product_material: [],
  product_purity: [],
  product_technology: [],
  product_skn: []
})
const like = async () => {
  try {
    const res = await love({
      id: props.info.id
    })
    props.info.is_love = !props.info.is_love
    Toast(res.msg)
  } catch {

  }

}
const img=(val)=>{
  ImagePreview([val])
}
const reset = () => {
  detial.value.product_material.forEach((ele, index) => {
    if (index == 0) {
      ele.check = true;
    } else {
      ele.check = false
    }

  })
  detial.value.product_purity.forEach((ele, index) => {
    if (index == 0) {
      ele.check = true;
    } else {
      ele.check = false
    }

  })
  detial.value.product_technology.forEach((ele, index) => {
    if (index == 0) {
      ele.check = true;
    } else {
      ele.check = false
    }

  })
  detial.value.product_skn.forEach((ele, index) => {
    if (index == 0) {
      ele.check = true;
    } else {
      ele.check = false
    }

  })
  detial.value.material_weight.forEach((ele, index) => {
    if (index == 0) {
      ele.check = true;
    } else {
      ele.check = false
    }

  })
}
const chooseItem = (val, index) => {
  console.log(val, index);
  detial.value[val].forEach((ele, i) => {
    if (index == i) {
      ele.check = !ele.check;
    } else {
      ele.check = false;
    }
  })
}
const value = ref()
const confirmAdd = async () => {
  if (detial.value.product_material.length && !detial.value.product_material.some(ele => ele.check)) {
    Toast("请选择产品材料")
  } else if (detial.value.product_purity.length && !detial.value.product_purity.some(ele => ele.check)) {
    Toast("请选择产品纯度")
  } else if (detial.value.product_technology.length && !detial.value.product_technology.some(ele => ele.check)) {
    Toast("请选择产品工艺")
  } else if (detial.value.product_skn.length && !detial.value.product_skn.some(ele => ele.check)) {
    Toast("请选择表面处理")
  } else if (detial.value.material_weight.length && !detial.value.material_weight.some(ele => ele.check)) {
    Toast("请选择重量")
  } else {
    cartEdit({
      type: "add",
      product_id: props.info.id,
      product_material: detial.value.product_material.filter(ele => ele.check)[0] ? detial.value.product_material.filter(ele => ele.check)[0].label : null,
      product_purity: detial.value.product_purity.filter(ele => ele.check)[0] ? detial.value.product_purity.filter(ele => ele.check)[0].label : null,
      product_technology: detial.value.product_technology.filter(ele => ele.check)[0] ? detial.value.product_technology.filter(ele => ele.check)[0].label : null,
      product_skn: detial.value.product_skn.filter(ele => ele.check)[0] ? detial.value.product_skn.filter(ele => ele.check)[0].label : null,
      material_weight: detial.value.material_weight.filter(ele => ele.check)[0] ? detial.value.material_weight.filter(ele => ele.check)[0].label : null,
      num: value.value,
    }).then(res => {
      if (res.code == 1) {
        show.value = false;
        Toast(res.msg)
        props.init()
        // router.push(`/Bus`)
      }

    })
  }
}
const addBus = () => {
  show.value = true
  product({
    product_id: props.info.id,
  }).then(res => {
    detial.value = {
      ...res.data,
      product_material: Array.isArray(res.data.product_material) ? res.data.product_material.map((ele, index) => ({
        label: ele,
        check: index == 0
      })) : [],
      product_purity: Array.isArray(res.data.product_purity) ? res.data.product_purity.map((ele, index) => ({
        label: ele,
        check: index == 0
      })) : [],
      product_technology: Array.isArray(res.data.product_technology) ? res.data.product_technology.map((ele, index) => ({
        label: ele,
        check: index == 0
      })) : [],
      product_skn: Array.isArray(res.data.product_skn) ? res.data.product_skn.map((ele, index) => ({
        label: ele,
        check: index == 0
      })) : [],
      material_weight: Array.isArray(res.data.material_weight) ? res.data.material_weight.map((ele, index) => ({
        label: ele,
        check: index == 0
      })) : [],
    };
    console.log(res)
  })

}
</script>

<style lang="less" scoped>
.item {
  width: 163px;
  //height: 220px;
  padding-bottom: 20px;
  margin-bottom: 20px;
  background: #FFFFFF;
  box-shadow: 0px 1px 12px 0px rgba(0, 0, 0, 0.08);
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 8px;
  position: relative;

  &__img {

    img {
      width: 115px;
      height: 115px;

    }
  }

  &__name {
    font-size: 16px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #333333;
    text-align: center;
    height: 16px;
    line-height: 16px;
    margin-top: 13px;
    width: 160px;
    overflow: hidden;

    white-space: nowrap;
    text-overflow: ellipsis;
  }

  &__bottom {
    display: flex;
    justify-content: center;
  }

  &__des {
    &-name {
      font-size: 13px;

      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #FFC200;
      margin-right: 6px;
    }

    &-unit {
      font-size: 8px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #808080;
    }
  }
}

.mask__chooseItem {
  &-title {
    font-size: 16px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #333333;
    margin-bottom: 13px;
  }
}

.mask__chooseItem-list {
  margin-bottom: 23px;
}

.mask {
  max-height: 90vh;
  overflow-x: hidden;
  position: relative;

}

.mask__number {
  display: flex;
  justify-content: space-between;
  font-size: 16px;
  height: 47px;
  align-items: center;
  border-top: 1px solid #eee;
  border-bottom: 1px solid #eee;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #333333;
}

.like {
  width: 19px;
  right: 10px;
  top: 10px;
  position: absolute;
}

.bay {
  margin-top: 7px;
  //margin-right: 33px;
  width: 18px;
  height: 18px;
}

.mask {
  width: 375px;
  box-sizing: border-box;
  //height: 559px;
  background: #FFFFFF;
  border-radius: 24px;
  padding: 19px;
  padding-bottom: 100px;

  &__title {
    display: flex;
    justify-content: center;
    font-size: 18px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #333333;
  }

  &__good {
    display: flex;
    margin-bottom: 25px;

    &-img {
      width: 78px;
      height: 78px;
      margin-right: 9px;
    }

    &-des {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
    }
  }
}

.des__name {
  font-size: 15px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #333333;
}

.des_des {
  font-size: 13px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #999999;
  line-height: 18px;
}

.list__item {
  width: 74px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-right: 10px;
  padding: 4px 11px;
  //height: 24px;
  background: #F1F1F1;
  border-radius: 12px;
  font-size: 12px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #666666;

  &.active {
    background: #D5E6FF;
  }
}

.mask__footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: fixed;
  left: 0;
  bottom: 0;
  right: 0;

  &-left {
    padding-left: 45px;
    font-size: 18px;
    box-sizing: border-box;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #888888;
  }

  &-right {
    text-align: center;
    line-height: 35px;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #FFFFFF;
    width: 99px;
    height: 35px;
    background: linear-gradient(90deg, #93E33A 0%, #5AC21B 100%);
    border-radius: 19px;
  }

  width: 374px;
  height: 49px;
  background: #FFFFFF;
  box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.72);
}

.mask__chooseItem-list {
  display: flex;
  flex-wrap: wrap;
}
</style>
